<?php
/**
*
*  百度音乐接口代理
*
*/


class mserver{

    /**
    * @param String txt 要搜索的字符串
    */

    public function search($txt){
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://sug.music.baidu.com/info/suggestion?from=0&word='.$txt);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $raw=curl_exec($ch);
        curl_close($ch);

        $res=json_decode($raw)->data->song;


        $resids=array_map(function($v){
            return $v->songid;
        },$res);

        $ch=curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://play.baidu.com/data/music/songinfo');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, ["songIds"=>implode(',',$resids)]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $raw=curl_exec($ch);
        curl_close($ch);
        
        $res=json_decode($raw)->data->songList;
        return $res;
    }

    public function getInfo($id){
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://play.baidu.com/data/cloud/songlink');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, ["songIds"=>$id]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $raw=curl_exec($ch);
        curl_close($ch);

        $res=json_decode($raw)->data->songList[0];

        return $res;
    }

}
?>